﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Net;
using System.Security.Cryptography.X509Certificates;
using uPLibrary.Networking.M2Mqtt;
using uPLibrary.Networking.M2Mqtt.Messages;
using System.Net.Security;

namespace m2mqtt_test
{
    class Program
    {
        static void messageReceive(object sender, MqttMsgPublishEventArgs e)
        {
            string msg = "Topic:" + e.Topic + "   Message:" + System.Text.Encoding.Default.GetString(e.Message);
            Console.WriteLine(msg);
        }
        //static bool cafileValidCallback(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
        //{
        //    string msg = "X509 链状态:";
        //    foreach (X509ChainStatus status in chain.ChainStatus)
        //    {
        //        msg += status.StatusInformation + "\n";
        //    }
        //    msg += "SSL策略问题：" + (int)sslPolicyErrors;

        //    Console.WriteLine(msg);

        //    if (sslPolicyErrors != SslPolicyErrors.None)
        //        return false;
        //    return true;
        //}

        static void Main(string[] args)
        {
            string brokerHostName = "gzhwedu.cn";
            int brokerPort = 1883;
            string clientId = "node01";
            //string username = "develop";
            //string password = "666666";
            string[] topic = { clientId + "/blink" };
            byte[] qosLevels = { MqttMsgBase.QOS_LEVEL_AT_MOST_ONCE };
            //string caPath = "C:/MqttSSL/ca.crt";
            //X509Certificate caCert = new X509Certificate(caPath);
            //Console.WriteLine(caCert.ToString(true) + "\n" + caCert.ToString());
            //Console.ReadKey();
            Console.WriteLine("------------------------分割线-------------------------------");
            //无SSL连接
            try
            {
                MqttClient client = new MqttClient(brokerHostName,brokerPort,false,null,null,MqttSslProtocols.None);
                //单向SSL通信
                //MqttClient client = new MqttClient(brokerHostName, brokerPort, true, caCert, null, MqttSslProtocols.TLSv1_2, new RemoteCertificateValidationCallback(cafileValidCallback));
                //消息接受
                client.MqttMsgPublishReceived += new MqttClient.MqttMsgPublishEventHandler(messageReceive);
                //连接Broker
                client.Connect(clientId);
                Console.WriteLine("连接成功!");
                //client.Connect(clientId, username, password);

                client.Subscribe(topic, qosLevels);
            }
            catch (System.Exception)
            {
                Console.WriteLine("连接失败!");
            }

        }
    }
}
